package com.blog.dao.impl;

import com.blog.dao.CommentDao;
import com.blog.pojo.Comment;
import com.blog.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    private QueryRunner queryRunner = new QueryRunner( DruidUtils.getDataSource());
    @Override
    public int addComment(Comment comment) {
        String sql = "INSERT INTO comments (articleId, userId, content, createdAt) VALUES (?,?,?,NOW())";
        Object[] params = {comment.getArticleId(), comment.getUserId(), comment.getContent()};

        try {
        int add=   queryRunner.update(sql, params);
            return add;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int deleteComment(int id) {
        String sql = "DELETE FROM comments WHERE id = ?";
        Object[] params = {id};
        int delete = 0;
        try {
            delete = queryRunner.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return delete;
    }

    @Override
    public int updateComment(Comment comment) {
     String sql = "UPDATE comments SET articleId= ?, userId = ?, content = ?, createdAt = NOW() WHERE id = ?";
     Object[] params = {comment.getArticleId(), comment.getUserId(), comment.getContent(), comment.getId()};
     try {
      int  update = queryRunner.update(sql, params);
         return update;
     } catch (SQLException e) {
         throw new RuntimeException(e);
     }

    }

    @Override
    public List<Comment> findAll() {
        String sql = "SELECT * FROM comments";
        try {
            List<Comment> commentList = queryRunner.query(sql, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Comment> findByAuthor(String author) {
        String sql = "SELECT * FROM comments WHERE author_id = ?";

        Object[] params = {author};
        try {
            List<Comment> commentList = queryRunner.query(sql, params, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Comment> findByTitle(String title) {
        String sql = "SELECT * FROM comments WHERE title = ?";
        Object[] params = {title};
        try {
            List<Comment> commentList = queryRunner.query(sql, params, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Comment findById(int id) {
        String sql = "SELECT * FROM comments WHERE id = ?";
        Object[] params = {id};


        try {
            Comment    comment = queryRunner.query(sql, params, new BeanHandler<Comment>(Comment.class));
            return comment;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }



    }

    @Override
    public List<Comment> findByUserIdAndArticleId(int userId, int articleId) {
        String sql="SELECT * from comments where articleId=? AND userId=?";
        Object[] params={userId,articleId};
        try {
            List<Comment>  listComment= queryRunner.query(sql,new BeanListHandler<Comment>(Comment.class), params);
            return listComment;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<Comment> findByArticleId(int articleId) {
        String sql="SELECT * from comments where articleId=? ORDER BY createdAt DESC";
        Object[] params={articleId};
        try {
            List<Comment>  listComment= queryRunner.query(sql,new BeanListHandler<Comment>(Comment.class), params);
            return listComment;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
